import java.util.Date;
import java.util.List;
import java.util.ArrayList;
public class Screening {
    private List<Ticket>soldTickets=new ArrayList<>();
    private List<Seat>flatSeats=new ArrayList<>();
    private String screeningId;
    private Movie movie;
    private String cinemaHall;
    private Date time;
    private List<Seat>seats=new ArrayList<>();
    private double price;
    private List<Seat>soldSeats=new ArrayList<>();
    private static List<Screening>screenings=new ArrayList<>();
    private boolean hasSoldTickets;
    public Screening(String screeningId, Movie movie, String cinemaHall, Date time, double price) {
        this.screeningId = screeningId;
        this.movie = movie;
        this.cinemaHall = cinemaHall;
        this.time = time;
        this.price = price;
        this.hasSoldTickets = false;
    }
    public List<List<Seat>> createSeatsForHall(int cinemaHall) {
        int rows = 0, cols = 0;
        switch (cinemaHall) {
            case 1:
                rows = 8;
                cols = 10;
                break; // 黄金厅
            case 2:
                rows = 10;
                cols = 12;
                break; // 白银厅
            case 3:
                rows = 7;
                cols = 8;
                break; // 翡翠厅
            case 4:
                rows = 7;
                cols = 8;
                break; // 钻石厅
            case 5:
                rows = 8;
                cols = 9;
                break; // 宝石厅
            default:
                throw new IllegalArgumentException("无效的厅编号: " + cinemaHall);
        }
        List<List<Seat>> seats = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            List<Seat> row = new ArrayList<>();
            for (int j = 0; j < cols; j++) {
                row.add(new Seat(i + 1, j + 1));
            }
            seats.add(row);
        }
        for(List<Seat>row:seats){
            for(Seat seat:row){
                flatSeats.add(seat);
            }
        }
        return seats;
    }
    private static Screening getScreeningById(String screeningId) {
        for (Screening screening : screenings) {
            if (screening.getScreeningId().equals(screeningId)) {
                return screening;
            }
        }
        return null;
    }
    public boolean sellTicket(int row, int col) {
        for (Seat seat : flatSeats) {
            if (seat.getRow() == row && seat.getCol() == col) {
                if (seat.isOccupied()) {
                    return false;
                }
                seat.setOccupied(true);
                List<Seat> seats = new ArrayList<>();
                seats.add(seat);
                Date buyTime = new Date();
                int totalTickets = 1;
                double totalAmount = price;
                int refundTickets = 0;
                double refundAmount = 0.0;
                Ticket ticket = new Ticket("T-" + System.currentTimeMillis(), this, seats, price, buyTime, totalTickets, totalAmount, refundTickets, refundAmount);
                soldTickets.add(ticket);
                return true;
            }
        }
        return false;
    }
    public List<Seat> getFlatSeats() {
        return flatSeats;
    }
    public void printSeats() {
        System.out.println("当前座位布局：");
        seats.forEach(seat -> System.out.println("[" + seat.getRow() + "," + seat.getCol() + "] 已售: " + seat.isOccupied()));
    }
    public boolean getHasSoldTicket(){
        return hasSoldTickets;
    }
    public int getSoldTicketsCount() {
        return soldTickets.size();
    }

    public double getTotalSales() {
        return soldTickets.stream().mapToDouble(Ticket::getTotalPrice).sum();
    }
    public Screening(int numSeats) {
        this.seats = new ArrayList<>();
        for (int i = 1; i <= numSeats; i++) {
            for(int j=1;j<=numSeats;j++) {
                seats.add(new Seat(i, j));
            }
        }
    }
    public List<Seat> getSeats() {
        return seats;
    }
    public void setSeats(List<Seat> seats){
        this.seats=seats;
    }
    public boolean hasSoldTickets(){
        return soldTickets!=null&&!soldTickets.isEmpty();
    }
    public String getScreeningId() {
        return screeningId;
    }
    public String getCinemaHall(){
        return cinemaHall;
    }
    public void setCinemaHall(String cinemahall){
        this.cinemaHall=cinemahall;
    }
    public Date getTime(){
        return this.time;
    }
    public void setTime(Date time){
        this.time=time;
    }
    public Movie getMovie() {
        return movie;
    }
    public void setMovie(Movie movie){
        this.movie=movie;
    }
    public double getPrice(){
        return price;
    }
    public void setPrice(double price){
        this.price=price;
    }

    public void setInfo(String cinemaHall, Date time, double price) {
        this.cinemaHall = cinemaHall;
        this.time = time;
        this.price = price;
    }
    public boolean hasTicketsSold() {
        for(Seat seat:seats) {
            if (seat.isOccupied()) {
                return true;
            }
        }
        return false;
    }
    public String toString() {
        return "场次ID: " + screeningId + " | 电影: " + movie.getTitle() + " | 影厅: " +cinemaHall + " | 时间: " + time + " | 票价: " + price + "元";
    }
    public void setHasSoldTickets() {//设置门票已售标记
        this.hasSoldTickets = true;
    }
}
